﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Drawing;


public partial class WorkSpace : System.Web.UI.Page
{
    DataSet dset = null, dsPermit = null, dst = null;
    DataSet usuarios = null, companies = null, accounts=null, usuClient;//lista de todos los usuarios
    rUser ruser, userinfo;
    public  string filterstatus, puser="";
    public  int sort;
    public long RWK = 0, PWK = 0;
    string where, where2, where3;
    int busqueda;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("../Default.aspx");
        }
        else
        {

            
            //mostrarPaginas();
            dsPermit = null; dst = null; dset = null;
            
            ruser = (rUser)Session["userid"];
            RE_GenericBean permit = new RE_GenericBean();
            permit = Utility.getPermisobyRuser(ruser, 1);// pagina Workspace
            PWK = permit.int64C1;
            RWK = permit.int64C2;
            busqueda = 0;
            string rxml = null;
            setVariablesSession();

            if (companies != null) { Session["companies"] = companies; }
            if (userinfo != null) { Session["userinfo"] = userinfo; }
            if (accounts != null) { Session["accounts"] = accounts; }

            try
            {
                linkcamara.Visible = false;
                //asdfaf
                RE_GenericBean permiso = Utility.getPermisobyRuser(ruser, 1);
                if ((permiso.int64C1 & 16) == 16)
                {
                    linkcamara.Visible = true;
                }
               
                bool perAll = false;
                if (((permiso.int64C1 & 2) == 2) )
                    perAll = true;
                
                string wheresearch="";
                where = ruser.getWhere();
                where2 = ruser.getWhere2();
                where3 = ruser.getWhere3(); 
                //trae en session where si viene de filter search
                if (Session["where"] != null) { 
                    wheresearch= (string)Session["where"];
                    where = wheresearch; busqueda = 1;
                    where2 = "";
                    where3 = "";
                    Session["where"] = null;
                }
                
                //si viene de multiple search
                string oldfilter = filterstatus;
                Session["oldfilter"] = oldfilter;
                string id = Request.QueryString["get"];
                if (id == null) { filterstatus = "default"; }
                if (id.Equals("pending")) { filterstatus = "pending";  Session["filterstatus"]=filterstatus; }
                if (id.Equals("assigned")) { filterstatus = "assigned"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("unassignedphotos")) { filterstatus = "unassignedphotos"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("assignedphotos")) { filterstatus = "assignedphotos"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("completed")) { filterstatus = "completed"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("cancelled")) { filterstatus = "cancelled"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("paid")) { filterstatus = "paid"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("unassigned")) { filterstatus = "unassigned"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("processed")) { filterstatus = "processed"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("processedphotos")) { filterstatus = "processedphotos"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("processedC")) { filterstatus = "processedC"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("photos")) { filterstatus = "photos"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("bpophotos")) { filterstatus = "bpophotos"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("ready")) { filterstatus = "ready"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("fail")) { filterstatus = "fail"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("pacific")) { filterstatus = "pacific"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("all")) { filterstatus = "all"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("allpending")) { if (perAll == true) { filterstatus = "allpending"; } else { filterstatus = "pending"; } Session["filterstatus"] = filterstatus; }
                if (id.Equals("previus")) { filterstatus = "previus"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("clientpending")) { filterstatus = "clientpending"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("clientcompleted")) { filterstatus = "clientcompleted"; Session["filterstatus"] = filterstatus; }
                if (id.Equals("clientcancelled")) { filterstatus = "clientcancelled"; Session["filterstatus"] = filterstatus; }
                /*
                long strpermiso = (ruser.getPermiso() & 8);
                if ((strpermiso) != 8 && filterstatus.Equals("paid")) { filterstatus = oldfilter; Session["filterstatus"] = filterstatus; }
               
                long strpermiso2 = (long.Parse(ruser.getPermisoUser().ToString()) & 8);
                if ((strpermiso2) != 8 && filterstatus.Equals("paid")) { filterstatus = oldfilter; Session["filterstatus"] = filterstatus; }
                 */
            }
            catch (Exception ex) { filterstatus = "default"; }

            if (!Page.IsPostBack)
            {
                DataSet usuGuest = (DataSet)(Session["usuInv"]);
                if (usuGuest != null)
                {
                    foreach (RE_GenericBean obj in userinfo.perfiles)
                    {
                        if (obj.intC1 == 7)
                        {
                            //imalert.Visible = true;
                            //lalert.Visible = true;
                        }
                    }
                }
                DateTime now = DateTime.Now;
                sort = 0;
                Session["sort"] = sort;
                hoy.Text = String.Format("{0:dddd, MMMM d, yyyy}", now).ToUpper();

                name.Text = ruser.getNombre().ToUpperInvariant() + " " + ruser.getApellido().ToUpperInvariant();
                long permiso = ruser.getPermiso();
                int perfilID = int.Parse(ruser.getPerfilId());
                logo.ImageUrl = ruser.getLogo();
                ArrayList per = ruser.perfiles;
                for (int i = 0; i < per.Count ; i++ ) {

                    RE_GenericBean p = (RE_GenericBean)per[i];
                    if (p.intC1 == 5 || p.intC1 == 2 || p.intC1 == 16 || p.intC1 == 3)
                    {
                        gv_detail.PageSize = 25;
                        lb_rows_limit.Items.Clear();
                        lb_rows_limit.Items.Add("25");
                        lb_rows_limit.Items.Add("50");
                        lb_rows_limit.Items.Add("75");
                        lb_rows_limit.Items.Add("100");
                    }
                    else {
                        gv_detail.PageSize = 100;
                        lb_rows_limit.Items.Clear();
                        lb_rows_limit.Items.Add("100");
                        lb_rows_limit.Items.Add("200");
                        lb_rows_limit.Items.Add("300");
                        lb_rows_limit.Items.Add("400");
                    }
                }
                if (!filterstatus.Equals("pending") && !filterstatus.Equals("default")) { PTitle.BackColor = System.Drawing.Color.LightGray; }
                int resp = restricted();
                if (resp == 1)
                {
                    // si viene de singlesearch
                    if (Session["masivo"] != null)
                    {
                        string where1 = (string)Session["masivo"];
                        PTitle.BackColor = System.Drawing.Color.LightGray;
                        RE_GenericBean permi = Utility.getPermisobyRuser(ruser, 1);
                        RE_GenericBean permi2 = Utility.getPermisobyRuser(ruser, 6);
                        dsPermit = DB.GetOrders(permi.int64C1, permi2.int64C1, "all", "", ruser.getAlias(), where1, "", "", 1, perfilID);
                        dset = DB.GetOrders(permi.int64C1, permi2.int64C1, "all", "", ruser.getAlias(), where1, "", "", 1, perfilID);

                        Session["masivo"] = null;
                    }
                    else if (Session["SingleSearch"] != null)
                    {
                        string where1 = (string)Session["wheress"] +" " ;
                        if (perfilID == 1)
                        {
                            where1 += " " + where;
                        }
                        PTitle.BackColor = System.Drawing.Color.LightGray;
                        RE_GenericBean permi = Utility.getPermisobyRuser(ruser, 1);
                        RE_GenericBean permi2 = Utility.getPermisobyRuser(ruser, 6);
                        dsPermit = DB.GetOrders(permi.int64C1, permi2.int64C1, "all", "", ruser.getAlias(), where1, "", "", 1, perfilID);
                        dset = DB.GetOrders(permi.int64C1, permi2.int64C1, "all", "", ruser.getAlias(), where1, "", "", 1, perfilID);
                        if (dsPermit != null)
                        {
                            if (dsPermit.Tables[0].Rows.Count == 0)
                            {
                                gv_detail.EmptyDataText = (string)Session["message"];
                            }
                        }
                        Session["SingleSearch"] = null;
                    }
                    else {
                        
                            if (Session["FilterSearch"]!=null)
                            {
                                PTitle.BackColor = System.Drawing.Color.LightGray;
                                Session["FilterSearch"] = null;
                            }
                            RE_GenericBean permi = Utility.getPermisobyRuser(ruser, 1);
                            RE_GenericBean permi2 = Utility.getPermisobyRuser(ruser, 6);
                            dsPermit = DB.GetOrders(permi.int64C1, permi2.int64C1, filterstatus, "", ruser.getAlias(), where, where2, where3, busqueda, perfilID);
                            dset = DB.GetOrders(permi.int64C1, permi2.int64C1, filterstatus, "", ruser.getAlias(), where, where2, where3, busqueda, perfilID);
                        }
                       
                    
                }
                permisos();
                
               
                Session["dsPermit"] = dsPermit;
                dst = DB.getStatus(perfilID.ToString());
                Session["dst"] = dst;
                dsPermit = permisoWK(dsPermit);
                Session["OrderList"] = dset;
                if (dsPermit != null)
                {
                    gv_detail.DataSource = dsPermit.Tables[0];
                    gv_detail.DataBind();
                    
                    //string whereDB = (string)Session["selectdb"];
                    string whereDB = (string)Session["wheredb"];
                    string mes = DateTime.Now.Month.ToString();
                    string dia = DateTime.Now.Day.ToString();
                    if (int.Parse(mes) < 10){mes = "0" + mes;}
                    if (int.Parse(dia) < 10){dia = "0" + dia;}

                    string fecha = mes + "/" + dia + "/" + DateTime.Now.Year + "')";
                    lb_today.Text = DB.getCountDue(fecha, whereDB).ToString();
                    lb_today_completed.Text = DB.getCountDue_processed(fecha, whereDB).ToString();
                    lb_assigned_today.Text = DB.getAssigned(fecha, whereDB).ToString();
                    lb_ready_today.Text = DB.getReady(fecha, whereDB).ToString();
                    lb_unassigned_today.Text = DB.getUnAssigned(fecha, whereDB).ToString();

                    lb_unassignedphotos_today.Text = DB.getUnAssignedPhotos(fecha, whereDB).ToString();
                    lb_assignedphotos_today.Text = DB.getAssignedPhotos(fecha, whereDB).ToString();
                    lb_today_completedphotos.Text = DB.getCountDue_processedPhotos(fecha, whereDB).ToString();

                    //lb_photos_today.Text = DB.getPhoto(fecha, whereDB).ToString();
                    string wherephoto = "where orden.status='PENDING'  and  (orden.ord_failed is null or orden.ord_failed = 'NO')  and (orden.ord_driver_done='YES' or orden.ord_driver_done= 'N/A')";
                    lb_photos.Text = DB.getPhoto(whereDB, wherephoto).ToString();
                    lb_processed.Text = DB.getProcessed(whereDB).ToString();
                    wherephoto = "where orden.status='PENDING'  and orden.company=1 ";
                    lb_photosag.Text = DB.getPhoto(whereDB, wherephoto).ToString();

                    //***************** COUNTER CLIENT **************************************
                    if (pcounterclient.Visible.ToString().ToUpper() == "TRUE")
                    {
                        lb_pending.Text = DB.getPendingClient(whereDB).ToString();
                        lb_completed.Text = DB.getCompletedClient(whereDB).ToString();
                        lb_cancelled.Text = DB.getCancelledClient(whereDB).ToString();
                    }
                    //***********************************************************************

                    mes = DateTime.Now.AddDays(1).Month.ToString();
                    dia = DateTime.Now.AddDays(1).Day.ToString();
                    if (int.Parse(mes) < 10) { mes = "0" + mes; }
                    if (int.Parse(dia) < 10) { dia = "0" + dia; }

                    fecha = mes + "/" + dia + "/" + DateTime.Now.AddDays(1).Year + "')";
                    lb_tomorrow.Text = DB.getCountDue(fecha, whereDB).ToString();
                    lb_tomorrow_completed.Text = DB.getCountDue_processed(fecha, whereDB).ToString();
                    lb_assigned_tomorrow.Text = DB.getAssigned(fecha, whereDB).ToString();
                    lb_ready_tomorrow.Text = DB.getReady(fecha, whereDB).ToString();
                    lb_unassigned_tomorrow.Text = DB.getUnAssigned(fecha, whereDB).ToString();

                    lb_unassignedphotos_tomorrow.Text = DB.getUnAssignedPhotos(fecha, whereDB).ToString();
                    lb_assignedphotos_tomorrow.Text = DB.getAssignedPhotos(fecha, whereDB).ToString();
                    lb_tomorrow_completedphotos.Text = DB.getCountDue_processedPhotos(fecha, whereDB).ToString();
                    //lb_photos_24hrs.Text = DB.getPhoto(fecha, whereDB).ToString();

                    mes = DateTime.Now.AddDays(2).Month.ToString();
                    dia = DateTime.Now.AddDays(2).Day.ToString();
                    if (int.Parse(mes) < 10) { mes = "0" + mes; }
                    if (int.Parse(dia) < 10) { dia = "0" + dia; }

                    fecha = mes + "/" + dia + "/" + DateTime.Now.AddDays(2).Year + "')";
                    lb_48hrs.Text = DB.getCountDue(fecha, whereDB).ToString();
                    lb_48hrs_completed.Text = DB.getCountDue_processed(fecha, whereDB).ToString();
                    lb_assigned_48hrs.Text = DB.getAssigned(fecha, whereDB).ToString();
                    lb_ready_48.Text = DB.getReady(fecha, whereDB).ToString();
                    lb_unassigned_48hrs.Text = DB.getUnAssigned(fecha, whereDB).ToString();

                    lb_unassignedphotos_48hrs.Text = DB.getUnAssignedPhotos(fecha, whereDB).ToString();
                    lb_assignedphotos_48hrs.Text = DB.getAssignedPhotos(fecha, whereDB).ToString();
                    lb_48hrs_completedphotos.Text = DB.getCountDue_processedPhotos(fecha, whereDB).ToString();
                    //lb_photos_48hrs.Text = DB.getPhoto(fecha, whereDB).ToString();

                    mes = DateTime.Now.AddDays(3).Month.ToString();
                    dia = DateTime.Now.AddDays(3).Day.ToString();
                    if (int.Parse(mes) < 10) { mes = "0" + mes; }
                    if (int.Parse(dia) < 10) { dia = "0" + dia; }

                    fecha = mes + "/" + dia + "/" + DateTime.Now.AddDays(3).Year + "')";
                    lb_72hrs.Text = DB.getCountDue(fecha, whereDB).ToString();
                    lb_72hrs_completed.Text = DB.getCountDue_processed(fecha, whereDB).ToString();
                    lb_assigned_72hrs.Text = DB.getAssigned(fecha, whereDB).ToString();
                    lb_ready_72.Text = DB.getReady(fecha, whereDB).ToString();
                    lb_unassigned_72hrs.Text = DB.getUnAssigned(fecha, whereDB).ToString();

                    lb_unassignedphotos_72hrs.Text = DB.getUnAssignedPhotos(fecha, whereDB).ToString();
                    lb_assignedphotos_72hrs.Text = DB.getAssignedPhotos(fecha, whereDB).ToString();
                    lb_72hrs_completedphotos.Text = DB.getCountDue_processedPhotos(fecha, whereDB).ToString();
                    //lb_photos_72hrs.Text = DB.getPhoto(fecha, whereDB).ToString();

                    mes = DateTime.Now.AddDays(4).Month.ToString();
                    dia = DateTime.Now.AddDays(4).Day.ToString();
                    if (int.Parse(mes) < 10) { mes = "0" + mes; }
                    if (int.Parse(dia) < 10) { dia = "0" + dia; }

                    fecha = mes + "/" + dia + "/" + DateTime.Now.AddDays(4).Year;
                   // fecha = fecha + "'" + " or orden.duedate > '" + fecha + "')";
                    fecha = fecha + "' or DATEDIFF (day, orden.duedate, getdate()) <-3)";
                    //string aux2 = fecha + "'" + " or orden.duedate > '" + fecha;
                    //string aux3 = fecha + "'" + " or orden.duedate > '" + fecha;

                        //(tablita.duedate='" + fecha + "')"  (tablita.duedate='24/10/2011' or tablita.duedate > '24/10/2011')

                    lb_96.Text = DB.getCountDue(fecha, whereDB).ToString();
                    lb_processed_96.Text = DB.getCountDue_processed(fecha, whereDB).ToString();
                    lb_assigned_96.Text = DB.getAssigned(fecha, whereDB).ToString();
                    lb_ready_96.Text = DB.getReady(fecha, whereDB).ToString();
                    lb_unassigned_96.Text = DB.getUnAssigned(fecha, whereDB).ToString();

                    lb_unassignedphotos_96.Text = DB.getUnAssignedPhotos(fecha, whereDB).ToString();
                    lb_assignedphotos_96.Text = DB.getAssignedPhotos(fecha, whereDB).ToString();
                    lb_processedphotos_96.Text = DB.getCountDue_processedPhotos(fecha, whereDB).ToString();
                    //lb_photos_96hrs.Text = DB.getPhoto(fecha, whereDB).ToString();

                    //llenando columna lates de contador
                    mes = DateTime.Now.AddDays(-1).Month.ToString();
                    dia = DateTime.Now.AddDays(-1).Day.ToString();
                    if (int.Parse(mes) < 10) { mes = "0" + mes; }
                    if (int.Parse(dia) < 10) { dia = "0" + dia; }
                    fecha = mes + "/" + dia + "/" + DateTime.Now.AddDays(-1).Year;
                    //fecha = fecha + "'" + " or orden.duedate < '" + fecha;
                    fecha = fecha + "' or DATEDIFF (day, orden.duedate, getdate()) > 0)" ;
                   

                    lb_lates.Text = DB.getCountDue(fecha, whereDB).ToString();
                    lb_processed_lates.Text = DB.getCountDue_processed(fecha, whereDB).ToString();
                    lb_assigned_lates.Text = DB.getAssigned(fecha, whereDB).ToString();
                    lb_ready_lates.Text = DB.getReady(fecha, whereDB).ToString();
                    lb_unassigned_lates.Text = DB.getUnAssigned(fecha, whereDB).ToString();

                    lb_unassignedphotos_lates.Text = DB.getUnAssignedPhotos(fecha, whereDB).ToString();
                    lb_assignedphotos_lates.Text = DB.getAssignedPhotos(fecha, whereDB).ToString();
                    lb_processedphotos_lates.Text = DB.getCountDue_processedPhotos(fecha, whereDB).ToString();
                    //lb_photos_lates.Text = DB.getPhoto(fecha, whereDB).ToString();
                    
                    string wherefail= " where orden.ord_failed = 'YES' and orden.status = 'PENDING'  ";
                    lb_failed.Text = DB.getFailed(whereDB, wherefail).ToString();
                    wherefail = " where  orden.status = 'PENDING' and orden.company<>1 ";
                    lb_failedag.Text = DB.getFailed(whereDB, wherefail).ToString();
                    //lb_failed_completed.Text = DB.getFailed_completed(where).ToString();

                    if (filterstatus == "photos")
                    {
                        lb_unassigned_today.Text = "0"; lb_unassigned_tomorrow.Text = "0"; lb_unassigned_48hrs.Text = "0"; lb_unassigned_72hrs.Text = "0"; lb_unassigned_96.Text = "0"; lb_unassigned_lates.Text = "0";
                        lb_assigned_today.Text = "0"; lb_assigned_tomorrow.Text = "0"; lb_assigned_48hrs.Text = "0"; lb_assigned_72hrs.Text = "0"; lb_assigned_96.Text = "0"; lb_assigned_lates.Text = "0";
                        lb_today_completed.Text = "0"; lb_tomorrow_completed.Text = "0"; lb_48hrs_completed.Text = "0"; lb_72hrs_completed.Text = "0"; lb_processed_96.Text = "0"; lb_processed_lates.Text = "0";
                        lb_ready_today.Text = "0"; lb_ready_tomorrow.Text = "0"; lb_ready_48.Text = "0"; lb_ready_72.Text = "0"; lb_ready_96.Text = "0"; lb_ready_lates.Text = "0";
                    }
                }
                RE_GenericBean alert = DB.hasAlerts(ruser.getAlias());

                if (alert!= null && alert.intC1 == 1)
                {
                    lb_alert.Text = alert.strC3;
                    alert_ModalPopupExtender.Show();

                }
                else
                {
                    lb_alert.Text = "None Alert!";
                    alert_ModalPopupExtender.Hide();

                }
            }
            else {
              dsPermit  = (DataSet)Session["dsPermit"] ;
              dst = (DataSet)Session["dst"];
              filterstatus = (string)Session["filterstatus"] ;
              sort = (int)Session["sort"];
              ruser = (rUser)Session["userid"];
              pintarContador();
                dsPermit = permisoWK(dsPermit);
                if (dsPermit != null)
                {
                    gv_detail.DataSource = dsPermit.Tables[0];
                    gv_detail.DataBind();
                }
                if (!tb_search.Text.Equals("")) { buscar(); }
              alert_ModalPopupExtender.Hide();
            }
           
        }

    }

    private DataSet permisoWK(DataSet orders)
    {
        long permitWK = 0;
        rUser user = (rUser)(Session["userid"]);
        RE_GenericBean permi = new RE_GenericBean();
        if (user != null)
        {
            permi = Utility.getPermisobyRuser(user, 1);
            permitWK = permi.int64C1;
        }

        if (orders != null)
        {
            if (orders.Tables[0].Columns.Contains("specialatttributes")) { orders.Tables[0].Columns.Remove("specialatttributes"); }
            if (orders.Tables[0].Columns.Contains("ord_failed")) { orders.Tables[0].Columns.Remove("ord_failed"); }
            if (orders.Tables[0].Columns.Contains("ord_failed_id")) { orders.Tables[0].Columns.Remove("ord_failed_id"); }
            if (orders.Tables[0].Columns.Contains("ord_processor_done")) { orders.Tables[0].Columns.Remove("ord_processor_done"); }
            if (orders.Tables[0].Columns.Contains("ord_driver_done")) { orders.Tables[0].Columns.Remove("ord_driver_done"); }
            if (orders.Tables[0].Columns.Contains("ord_qc_done")) { orders.Tables[0].Columns.Remove("ord_qc_done"); }
            if (orders.Tables[0].Columns.Contains("Order id")) { orders.Tables[0].Columns.Remove("Order id"); }
            if (orders.Tables[0].Columns.Contains("ord_rv")) { orders.Tables[0].Columns.Remove("ord_rv"); }
            
            if ((permitWK & 2097152) != 2097152) if (orders.Tables[0].Columns.Contains("damages")) { orders.Tables[0].Columns.Remove("damages"); }
            if ((permitWK & 4194304) != 4194304) if (orders.Tables[0].Columns.Contains("Due Date")) { orders.Tables[0].Columns.Remove("Due Date"); }
            if ((permitWK & 8388608) != 8388608) if (orders.Tables[0].Columns.Contains("Due Time")) { orders.Tables[0].Columns.Remove("Due Time"); }
            if ((permitWK & 16777216) != 16777216) if (orders.Tables[0].Columns.Contains("Client")) { orders.Tables[0].Columns.Remove("Client"); }
            if ((permitWK & 33554432) != 33554432) if (orders.Tables[0].Columns.Contains("Company")) { orders.Tables[0].Columns.Remove("Company"); }
            if ((permitWK & 67108864) != 67108864) if (orders.Tables[0].Columns.Contains("Account")) { orders.Tables[0].Columns.Remove("Account"); }
            if ((permitWK & 134217728) != 134217728) if (orders.Tables[0].Columns.Contains("Type")) { orders.Tables[0].Columns.Remove("Type"); }
            if ((permitWK & 268435456) != 268435456) if (orders.Tables[0].Columns.Contains("Street No.")) { orders.Tables[0].Columns.Remove("Street No."); }
            if ((permitWK & 536870912) != 536870912) if (orders.Tables[0].Columns.Contains("Street Address")) { orders.Tables[0].Columns.Remove("Street Address"); }
            if ((permitWK & 1073741824) != 1073741824) if (orders.Tables[0].Columns.Contains("City")) { orders.Tables[0].Columns.Remove("City"); }
            if ((permitWK & 2147483648) != 2147483648) if (orders.Tables[0].Columns.Contains("State")) { orders.Tables[0].Columns.Remove("State"); }
            if ((permitWK & 4294967296) != 4294967296) if (orders.Tables[0].Columns.Contains("County")) { orders.Tables[0].Columns.Remove("County"); }
            if ((permitWK & 8589934592) != 8589934592) if (orders.Tables[0].Columns.Contains("Zip")) { orders.Tables[0].Columns.Remove("Zip"); }
            if ((permitWK & 17179869184) != 17179869184) if (orders.Tables[0].Columns.Contains("Proc")) { orders.Tables[0].Columns.Remove("Proc"); }//orders.Tables[0].Columns.Remove("ord_processor_done"); }
            if ((permitWK & 34359738368) != 34359738368) if (orders.Tables[0].Columns.Contains("Drv")) { orders.Tables[0].Columns.Remove("Drv"); }//orders.Tables[0].Columns.Remove("ord_driver_done"); }
            if ((permitWK & 68719476736) != 68719476736) if (orders.Tables[0].Columns.Contains("ord_qc")) { orders.Tables[0].Columns.Remove("ord_qc"); }//orders.Tables[0].Columns.Remove("ord_qc_done"); }
            if ((permitWK & 137438953472) != 137438953472) if (orders.Tables[0].Columns.Contains("PI Comments")) { orders.Tables[0].Columns.Remove("PI Comments"); }
            if ((permitWK & 64) != 64) if (orders.Tables[0].Columns.Contains("POC Comments")) { orders.Tables[0].Columns.Remove("POC Comments"); }
            if ((permitWK & 274877906944) != 274877906944) if (orders.Tables[0].Columns.Contains("Status")) { orders.Tables[0].Columns.Remove("Status"); }
            if ((permitWK & 549755813888) != 549755813888) if (orders.Tables[0].Columns.Contains("Appt")) { orders.Tables[0].Columns.Remove("Appt"); }
            if ((permitWK & 1099511627776) != 1099511627776) if (orders.Tables[0].Columns.Contains("Apptime")) { orders.Tables[0].Columns.Remove("Apptime"); }
            if ((permitWK & 2199023255552) != 2199023255552) if (orders.Tables[0].Columns.Contains("Appt Comments")) { orders.Tables[0].Columns.Remove("Appt Comments"); }
            if ((permitWK & 4398046511104) != 4398046511104) if (orders.Tables[0].Columns.Contains("Drv. Comments")) { orders.Tables[0].Columns.Remove("Drv. Comments"); }
        }
        return orders;
    }

    private void setVariablesSession()
    {
        string rxmlcompanies = null;
        com.bpophotos.www.Service service = new com.bpophotos.www.Service();
        /*try
        {
                
            rxml = service.getPerfiles(1);
            DataSet perfiles = Utility.getPerfiles(rxml);
            Session["Perfiles"] = perfiles;
            Utility.writeError("WK getPerfiles(1) "+rxml);
            usuarios = Utility.getUsuarios(rxml);
        }
        catch (Exception ex) { Utility.writeError(ex.Message); }
        if (usuarios != null) { Session["Usuarios"] = usuarios; }*/

        try
        {

            rxmlcompanies = service.getCompanies();
            companies = Utility.getCompanies(rxmlcompanies);

            puser = ruser.getUsuarioId().ToString();
            string info = service.getTodosDatosUsuario(puser);
            userinfo = new rUser();
            userinfo = Utility.getRUser(info);

            string xmlaccount = service.getAccounts();
            accounts = Utility.getAccounts(xmlaccount);

            //cargo los datos de los clientes para llenar el wk
            usuClient = (DataSet)Session["usuClient"];
            if (usuClient == null)
            {
                string rxml1 = service.getPerfiles(1,1);
                DataSet perfiles = Utility.getPerfiles(rxml1);
                Session["Perfiles"] = perfiles;
                usuClient = Utility.getUsuClient(rxml1);
            }
            if (usuClient != null) { Session["usuClient"] = usuClient; }

        }
        catch (Exception ex) { Utility.writeError(ex.Message); }
        finally {
            if (service != null) { service.Dispose(); }

        }
    }
    protected void gv_detail_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit") {
            int index = Convert.ToInt32(e.CommandArgument);
            int id = int.Parse(gv_detail.Rows[index].Cells[3].Text);
            Response.Redirect("ODetils.aspx?id=" + id);
        }
    }
    
    protected void lb_rows_limit_SelectedIndexChanged(object sender, EventArgs e)
    {
        int cantidad = int.Parse(lb_rows_limit.SelectedValue);
        gv_detail.PageSize = cantidad;
        if (Session["dsPermit"] != null)
            dsPermit = (DataSet)Session["dsPermit"];

        if (dsPermit != null)
        {         
            gv_detail.DataSource = dsPermit.Tables["Tabla"];
            gv_detail.DataBind();
        }
    }
    protected void gv_detail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
         ruser = (rUser)Session["userid"];
         int ready = 0;
         
        /*if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header) 
        {
            if (dsPermit != null)
            {
                
                if (dsPermit.Tables[0].Columns.Contains("specialatttributes")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("specialatttributes")].Style.Add("display", "none");}
                if (dsPermit.Tables[0].Columns.Contains("ord_failed")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("ord_failed")].Style.Add("display", "none");}
                if (dsPermit.Tables[0].Columns.Contains("ord_failed_id")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("ord_failed_id")].Style.Add("display", "none");}
                if (dsPermit.Tables[0].Columns.Contains("ord_processor_done")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("ord_processor_done")].Style.Add("display", "none");}
                if (dsPermit.Tables[0].Columns.Contains("ord_driver_done")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("ord_driver_done")].Style.Add("display", "none");}
                if (dsPermit.Tables[0].Columns.Contains("ord_qc_done")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("ord_qc_done")].Style.Add("display", "none");}
                if (dsPermit.Tables[0].Columns.Contains("Order id")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Order id")].Style.Add("display", "none");}
                if (dsPermit.Tables[0].Columns.Contains("ord_rv")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("ord_rv")].Style.Add("display", "none");}
            }
        }*/

        if (e.Row.RowType == DataControlRowType.Header) 
        {
            if (dsPermit != null)
            {
                if (dsPermit.Tables[0].Columns.Contains("damages")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("damages")].Text = string.Empty; }
                e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text = string.Empty;
                /*if (dsPermit.Tables[0].Columns.Contains("specialatttributes")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("specialatttributes")].Text = "";}
                if (dsPermit.Tables[0].Columns.Contains("ord_failed")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("ord_failed")].Text = "";}
                if (dsPermit.Tables[0].Columns.Contains("ord_failed_id")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("ord_failed_id")].Text = "";}
                if (dsPermit.Tables[0].Columns.Contains("ord_processor_done")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("ord_processor_done")].Text = "";}
                if (dsPermit.Tables[0].Columns.Contains("ord_driver_done")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("ord_driver_done")].Text = "";}
                if (dsPermit.Tables[0].Columns.Contains("ord_qc_done")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("ord_qc_done")].Text = "";}
                if (dsPermit.Tables[0].Columns.Contains("Order id")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Order id")].Text = "";}
                if (dsPermit.Tables[0].Columns.Contains("ord_rv")){e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("ord_rv")].Text = "";}*/

                e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Order #")].Text = "OrderID";
                e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Order #")].Style.Add("style", "text-decoration:none; ");
                e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Order #")].HorizontalAlign = HorizontalAlign.Center;
                e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Order #")].Style.Add("style", "color:black; ");

                if (dsPermit.Tables[0].Columns.Contains("Due Date")) 
                {
                    int contF = dsPermit.Tables[0].Columns.IndexOf("Due Date");
                    e.Row.Cells[contF].Text = "Date";
                    e.Row.Cells[contF].Style.Add("style", "text-decoration:none; ");
                }
                if (dsPermit.Tables[0].Columns.Contains("Due Time"))
                {
                    int contF = dsPermit.Tables[0].Columns.IndexOf("Due Time");
                    e.Row.Cells[contF].Text = "Time";
                    e.Row.Cells[contF].Style.Add("style", "text-decoration:none; ");
                }
                if (dsPermit.Tables[0].Columns.Contains("Street No."))
                {
                    int contF = dsPermit.Tables[0].Columns.IndexOf("Street No.");
                    e.Row.Cells[contF].Text = "St. #";
                    e.Row.Cells[contF].Style.Add("style", "text-decoration:none; ");
                }
                if (dsPermit.Tables[0].Columns.Contains("Street Address"))
                {
                    int contF = dsPermit.Tables[0].Columns.IndexOf("Street Address");
                    e.Row.Cells[contF].Text = "Street Address  ";
                    e.Row.Cells[contF].Style.Add("style", "text-decoration:none; ");
                }
                if (dsPermit.Tables[0].Columns.Contains("Zip"))
                {
                    int contF = dsPermit.Tables[0].Columns.IndexOf("Zip");
                    e.Row.Cells[contF].Text = "Zip";
                    e.Row.Cells[contF].Style.Add("style", "text-decoration:none; ");
                }
                if (dsPermit.Tables[0].Columns.Contains("Proc")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Proc")].Style.Add("style", "text-decoration:none; "); }
                if (dsPermit.Tables[0].Columns.Contains("Drv")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Drv")].Style.Add("style", "text-decoration:none; "); }
                if (dsPermit.Tables[0].Columns.Contains("ord_qc"))
                {
                    int contF = dsPermit.Tables[0].Columns.IndexOf("ord_qc");
                    e.Row.Cells[contF].Text = "QC";
                    e.Row.Cells[contF].Style.Add("style", "text-decoration:none; ");
                }
                if (dsPermit.Tables[0].Columns.Contains("PI Comments")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("PI Comments")].Style.Add("style", "text-decoration:none; "); }
                if (dsPermit.Tables[0].Columns.Contains("POC Comments")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("POC Comments")].Style.Add("style", "text-decoration:none; "); }
                if (dsPermit.Tables[0].Columns.Contains("Status")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Status")].Style.Add("style", "text-decoration:none; "); }
                if (dsPermit.Tables[0].Columns.Contains("Apptime"))
                {
                    int contF = dsPermit.Tables[0].Columns.IndexOf("Apptime");
                    e.Row.Cells[contF].Text = "Time";
                    e.Row.Cells[contF].Style.Add("style", "text-decoration:none; ");
                }
                if (dsPermit.Tables[0].Columns.Contains("Appt Comments")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Appt Comments")].Style.Add("style", "text-decoration:none; "); }
                if (dsPermit.Tables[0].Columns.Contains("Drv. Comments")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Drv. Comments")].Style.Add("style", "text-decoration:none; "); }
                for (int k = 0; k < dsPermit.Tables[0].Columns.Count; k++)
                {
                    e.Row.Cells[k].HorizontalAlign = HorizontalAlign.Left;
                }
                e.Row.HorizontalAlign = HorizontalAlign.Left;
                e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Paid")].Visible = false;

//************************************* ANCHO DE COMULMNAS ************************************
    e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Order #")].Attributes.Add("Style", " width:48px;"); 
    if (dsPermit.Tables[0].Columns.Contains("Due Date")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Due Date")].Attributes.Add("Style", " width:35px;"); }
    if (dsPermit.Tables[0].Columns.Contains("Client")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Client")].Attributes.Add("Style", " width:35px;"); }
    if (dsPermit.Tables[0].Columns.Contains("Account")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Account")].Attributes.Add("Style", " width:45px;"); }
    if (dsPermit.Tables[0].Columns.Contains("Type")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Type")].Attributes.Add("Style", " width:42px;"); }
    if (dsPermit.Tables[0].Columns.Contains("Street No.")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Street No.")].Attributes.Add("Style"," width:50px;"); }
    if (dsPermit.Tables[0].Columns.Contains("Street Address")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Street Address")].Attributes.Add("Style"," width:130px;"); }
    if (dsPermit.Tables[0].Columns.Contains("City")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("City")].Attributes.Add("Style"," width:120px;"); }
    if (dsPermit.Tables[0].Columns.Contains("State")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("State")].Attributes.Add("Style"," width:30px;"); }
    if (dsPermit.Tables[0].Columns.Contains("Zip")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Zip")].Attributes.Add("Style", " width:40px;"); }
    if (dsPermit.Tables[0].Columns.Contains("County")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("County")].Attributes.Add("Style", " width:38px;"); }
    if (dsPermit.Tables[0].Columns.Contains("Proc")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Proc")].Attributes.Add("Style", " width:30px;"); }
    if (dsPermit.Tables[0].Columns.Contains("Drv")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Drv")].Attributes.Add("Style", " width:30px;"); }
    if (dsPermit.Tables[0].Columns.Contains("ord_qc")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("ord_qc")].Attributes.Add("Style", " width:30px;"); }
    if (dsPermit.Tables[0].Columns.Contains("PI Comments")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("PI Comments")].Attributes.Add("Style", " width:250px;"); }
    if (dsPermit.Tables[0].Columns.Contains("POC Comments")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("POC Comments")].Attributes.Add("Style", " width:250px;"); }
    if (dsPermit.Tables[0].Columns.Contains("Appt")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Appt")].Attributes.Add("Style", " width:35px;"); }
    if (dsPermit.Tables[0].Columns.Contains("Apptime")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Apptime")].Attributes.Add("Style", " width:50px;"); }
    if (dsPermit.Tables[0].Columns.Contains("Appt Comments")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Appt Comments")].Attributes.Add("Style", " width:240px;"); }
    if (dsPermit.Tables[0].Columns.Contains("Drv. Comments")) { e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Drv. Comments")].Attributes.Add("Style", " width:240px;"); }
//***********************************************************************************************
            }
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (dsPermit != null)
            {
                try
                {
                    if (dset == null)
                    {
                        dset = (DataSet)Session["OrderList"];
                    }
                    e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Visible = false;
                    e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Paid")].Visible = false;

                    int contpaid   = dsPermit.Tables[0].Columns.IndexOf("Paid");
                    string t= e.Row.Cells[contpaid].Text;
                    if (e.Row.Cells[contpaid].Text.Equals("0"))
                    {
                        e.Row.ForeColor = System.Drawing.Color.Gray;
                       // e.Row.BackColor = System.Drawing.Color.Red;
                    }

                    /*string auxi, id1, id2;
                    id1 = dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["Order #"].ToString();
                    id2 = e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Order #")].Text;*/
                    e.Row.HorizontalAlign = HorizontalAlign.Left;
                    if (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["specialatttributes"].ToString() == "YES")
                    {
                        e.Row.Font.Bold = true;
                        e.Row.Font.Size = 8;
                    }

                    for (int i = 0; i < dsPermit.Tables[0].Columns.Count; i++)
                    {
                        if (e.Row.Cells[i].Text.Equals("&nbsp;"))
                        {
                            e.Row.Cells[i].Text = "";
                        }
                    }

                    HyperLink link = new HyperLink();
                    link.Text = e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Order #")].Text;


                  //link.NavigateUrl = "http://localhost:7591/bpophotos/admin/ODetails.aspx?id=" + dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["Order id"].ToString();
                    link.NavigateUrl = "http://www.bpophotos.com/admin/ODetails.aspx?id=" + dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["Order id"].ToString();

                    e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Order #")].Controls.Add(link);

                    string estado = "";
                    if (dsPermit.Tables[0].Columns.Contains("Status"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("Status");
                        e.Row.Cells[contf].Text = e.Row.Cells[contf].Text.ToUpper();
                        estado = e.Row.Cells[contf].Text;
                    }
                    string hoy = DateTime.Now.ToString("MM/dd/yyyy");

                    if (estado == "COMPLETED" || estado == "PAID") { e.Row.BackColor = System.Drawing.Color.LightGray; }
                    if (dsPermit.Tables[0].Columns.Contains("Due Date"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("Due Date");
                        if (e.Row.Cells[contf].Text.Equals(hoy))
                            e.Row.Cells[contf].ForeColor = System.Drawing.Color.Red;
                        e.Row.Cells[contf].Text = Utility.formatDateMMDD(e.Row.Cells[contf].Text);
                    }
                    if (dsPermit.Tables[0].Columns.Contains("PI Comments"))
                        e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("PI Comments")].Attributes.Add("title", e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("PI Comments")].Text);
                    if (dsPermit.Tables[0].Columns.Contains("Appt Comments"))
                        e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Appt Comments")].Attributes.Add("title", e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Appt Comments")].Text);
                    if (dsPermit.Tables[0].Columns.Contains("POC Comments"))
                        e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("POC Comments")].Attributes.Add("title", e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("POC Comments")].Text);
                    if (dsPermit.Tables[0].Columns.Contains("Drv. Comments"))
                        e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Drv. Comments")].Attributes.Add("title", e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Drv. Comments")].Text);
                    if (dsPermit.Tables[0].Columns.Contains("Due Time"))
                        e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Due Time")].Text = Utility.Hora24horastoFormatoAMPM(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Due Time")].Text).strC1 + Utility.Hora24horastoFormatoAMPM(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Due Time")].Text).strC2;
                    if (dsPermit.Tables[0].Columns.Contains("Client"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("Client");
                        e.Row.Cells[contf].Text = e.Row.Cells[contf].Text.ToUpper();
                        e.Row.Cells[contf].HorizontalAlign = HorizontalAlign.Center;
                        //escribiendo inicial del cliente de la orden
                        if (usuClient != null)
                        {
                            for (int i = 0; i < usuClient.Tables[0].Rows.Count; i++)
                            {
                                string dsclient = usuClient.Tables[0].Rows[i][0].ToString();
                                if (e.Row.Cells[contf].Text.Equals(dsclient))
                                {
                                    e.Row.Cells[contf].Text = usuClient.Tables[0].Rows[i][6].ToString();
                                    break;
                                }
                            }
                        }
                    }
                    if (dsPermit.Tables[0].Columns.Contains("Account"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("Account");
                        e.Row.Cells[contf].Text = e.Row.Cells[contf].Text.ToUpper();
                        //escribiendo inicial account
                        if (accounts != null)
                        {
                            for (int i = 0; i < accounts.Tables[0].Rows.Count; i++)
                            {
                                string dsacc = accounts.Tables[0].Rows[i][0].ToString();
                                if (e.Row.Cells[contf].Text.Equals(dsacc))
                                {
                                    e.Row.Cells[contf].Text = accounts.Tables[0].Rows[i][2].ToString();
                                    e.Row.Cells[contf].Attributes.Add("title", accounts.Tables[0].Rows[i]["acc_nombre"].ToString());
                                    break;
                                }
                            }
                        }
                    }
                    if (dsPermit.Tables[0].Columns.Contains("Type"))
                        e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Type")].Text = e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Type")].Text.ToUpper();
                    if (dsPermit.Tables[0].Columns.Contains("Street No."))
                        e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Street No.")].Text = e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Street No.")].Text.ToUpper();
                    if (dsPermit.Tables[0].Columns.Contains("Street Address"))
                        e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Street Address")].Text = Utility.recortarString(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Street Address")].Text.ToUpper(), 25);
                    if (dsPermit.Tables[0].Columns.Contains("City"))
                        e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("City")].Text = Utility.recortarString(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("City")].Text.ToUpper(), 14);
                    if (dsPermit.Tables[0].Columns.Contains("State"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("State");
                        e.Row.Cells[contf].Text = e.Row.Cells[contf].Text.ToUpper();
                        e.Row.Cells[contf].HorizontalAlign = HorizontalAlign.Center;
                    }
                    if (dsPermit.Tables[0].Columns.Contains("County"))
                        e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("County")].Text = e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("County")].Text.ToUpper();
                    if (dsPermit.Tables[0].Columns.Contains("Zip"))
                        e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Zip")].Text = e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Zip")].Text.ToUpper();

                    if (dsPermit.Tables[0].Columns.Contains("PI Comments"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("PI Comments");
                        if (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["specialatttributes"].ToString() == "YES")
                            e.Row.Cells[contf].Text = Utility.recortarString(e.Row.Cells[contf].Text, 20);
                        else
                            e.Row.Cells[contf].Text = Utility.recortarString(e.Row.Cells[contf].Text.ToUpper(), 22);//pi comments
                    }
                    if (dsPermit.Tables[0].Columns.Contains("Appt Comments"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("Appt Comments");
                        if (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["specialatttributes"].ToString() == "YES")
                            e.Row.Cells[contf].Text = Utility.recortarString(e.Row.Cells[contf].Text.ToUpper(), 23);
                        else
                            e.Row.Cells[contf].Text = Utility.recortarString(e.Row.Cells[contf].Text.ToUpper(), 25);//appt comments
                    }

                    if (dsPermit.Tables[0].Columns.Contains("Company"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("Company");
                        e.Row.Cells[contf].HorizontalAlign = HorizontalAlign.Left;//company
                        //escribiendo nombre compania
                        if (companies != null)
                        {
                            for (int i = 0; i < companies.Tables[0].Rows.Count; i++)
                            {
                                string dscompany = companies.Tables[0].Rows[i][0].ToString();
                                if (e.Row.Cells[contf].Text.Equals(dscompany))
                                {
                                    e.Row.Cells[contf].Text = companies.Tables[0].Rows[i][1].ToString();
                                    break;
                                }
                            }
                        }
                    }

                    if (dsPermit.Tables[0].Columns.Contains("Appt"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("Appt");
                        e.Row.Cells[contf].HorizontalAlign = HorizontalAlign.Center;
                        try
                        {
                            e.Row.Cells[contf].Text = Utility.getsrtDate(e.Row.Cells[contf].Text);
                            e.Row.Cells[contf].Text = Utility.formatDateMMDD(e.Row.Cells[contf].Text);
                        }
                        catch (Exception ex){e.Row.Cells[contf].Text = "";}
                    }
                    if (dsPermit.Tables[0].Columns.Contains("damages"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("damages");
                        if (e.Row.Cells[contf].Text == "YES")
                        {
                            e.Row.Cells[contf].Text = "<img src=\"..\\img\\redstar.gif \" align=\"right\" >";
                        }
                        else
                        {
                            e.Row.Cells[contf].Text = "";
                        }
                    }

                    DataSet process = DB.getProcessProduct(1);
                    string processname = null;

                    if (process != null)
                    {
                        processname = process.Tables[0].Rows[0][0].ToString();
                    }
                    /*int intCampoVendor = DB.getCampoID(processname, "VENDOR");
                    int intCampoDone = DB.getCampoID(processname, "DONE");
                    e.Row.Cells[17].Text = intCampoVendor.ToString().ToUpper();
                    e.Row.Cells[18].Text = intCampoDone.ToString().ToUpper();*/
                    //si estan null las variables de session de cliente, compania 
                    usuClient = (DataSet)Session["usuClient"];
                    companies = (DataSet)Session["companies"];
                    accounts = (DataSet)Session["accounts"];
                    if (usuClient == null || companies == null || accounts == null)
                    {
                        setVariablesSession();
                    }

                    if (dsPermit.Tables[0].Columns.Contains("Proc"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("Proc");
                        e.Row.Cells[contf].HorizontalAlign = HorizontalAlign.Center;
                        if (estado == "PENDING" && (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_processor_done"].ToString() == "N/A" || dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_processor_done"].ToString() == "YES"))
                        {
                            e.Row.Cells[contf].Attributes.Add("Style", "background: url(../img/circlegreen.png) no-repeat center center;");
                        }
                    }

                    if (dsPermit.Tables[0].Columns.Contains("Drv"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("Drv");
                        e.Row.Cells[contf].HorizontalAlign = HorizontalAlign.Center;
                        if (estado == "PENDING" && (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_driver_done"].ToString() == "N/A" || dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_driver_done"].ToString() == "YES"))
                        {
                            e.Row.Cells[contf].Attributes.Add("Style", "background: url(../img/circlegreen.png) no-repeat center center;");
                        }
                    }

                    if (dsPermit.Tables[0].Columns.Contains("ord_qc"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("ord_qc");
                        e.Row.Cells[contf].HorizontalAlign = HorizontalAlign.Center;
                        if (estado == "PENDING" && (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_qc_done"].ToString() == "N/A" || dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_qc_done"].ToString() == "YES"))
                        {
                            e.Row.Cells[contf].Attributes.Add("Style", "background: url(../img/circlegreen.png) no-repeat center center;");
                        }
                    }

                    if (dsPermit.Tables[0].Columns.Contains("Status"))
                    {
                        int contf = dsPermit.Tables[0].Columns.IndexOf("Status");
                        if ((dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_failed"].ToString() == "YES") && (estado == "PENDING"))//Si la orden es failed
                        {
                            e.Row.Cells[contf].Text = "<img src=\"..\\img\\Failed.png \" align=\"center\" >";
                            e.Row.Cells[contf].HorizontalAlign = HorizontalAlign.Left;
                        }
                        else if (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_failed"].ToString() == "NO" || dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_failed"].ToString() == "" || dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_failed"].ToString().ToUpper() == "&NBSP;")// si la orden no es failed
                        {
                            if ((estado == "PENDING") && (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_processor_done"].ToString() == "YES" || dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_processor_done"].ToString() == "N/A") && (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_driver_done"].ToString() == "YES" || dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_driver_done"].ToString() == "N/A"))//Si es ready
                            {
                                e.Row.Cells[contf].Text = "<img src=\"..\\img\\Readybutton.png \" align=\"center\" >"; 
                                e.Row.Cells[contf].HorizontalAlign = HorizontalAlign.Left;
                            }
                        }

                        if (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_rv"].ToString().Trim() == "YES" && (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_failed"].ToString() != "YES") && (estado == "PENDING"))
                        {
                            e.Row.Cells[contf].Text = "<img src=\"..\\img\\Revisions.png \" align=\"center\" >"; 
                            e.Row.Cells[contf].HorizontalAlign = HorizontalAlign.Left;
                        }
                        else if (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_rv"].ToString().Trim() == "NO" && (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_failed"].ToString() != "YES") && (estado == "PENDING"))
                        {
                            e.Row.Cells[contf].Text = "<img src=\"..\\img\\Revdone.png \" align=\"center\" >"; 
                            e.Row.Cells[contf].HorizontalAlign = HorizontalAlign.Left;
                        }
                    }

                    if ((dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_failed"].ToString() == "YES") && (estado == "PENDING"))
                    {
                        DataSet failaux = (DataSet)Session["dsPermit"];
                        for (int i = 0; i < failaux.Tables[1].Rows.Count; i++)
                        {
                            if (dset.Tables[0].Rows[int.Parse(e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Indice")].Text)]["ord_failed_id"].ToString() == failaux.Tables[1].Rows[i]["order_fail_id"].ToString())
                            {
                                if (failaux.Tables[1].Rows[i]["fail_id"].ToString() == "1" || failaux.Tables[1].Rows[i]["fail_id"].ToString() == "2")
                                {
                                    if (dsPermit.Tables[0].Columns.Contains("Proc"))
                                    {
                                        int contf = dsPermit.Tables[0].Columns.IndexOf("Proc");
                                        e.Row.Cells[contf].Text = failaux.Tables[1].Rows[i]["agent"].ToString();
                                        if (failaux.Tables[1].Rows[i]["completed"].ToString().Trim() == "NO")
                                        {
                                            e.Row.Cells[contf].Attributes.Add("Style", "background: none;");
                                        }
                                    }
                                }
                                else if (failaux.Tables[1].Rows[i]["fail_id"].ToString() == "3")
                                {
                                    if (dsPermit.Tables[0].Columns.Contains("Drv"))
                                    {
                                        int contf = dsPermit.Tables[0].Columns.IndexOf("Drv");
                                        e.Row.Cells[contf].Text = failaux.Tables[1].Rows[i]["agent"].ToString();
                                        if (failaux.Tables[1].Rows[i]["completed"].ToString().Trim() == "NO")
                                        {
                                            e.Row.Cells[contf].Attributes.Add("Style", "background: none;");
                                        }
                                    }
                                }
                                else if (failaux.Tables[1].Rows[i]["fail_id"].ToString() == "4")
                                {
                                    if (dsPermit.Tables[0].Columns.Contains("ord_qc"))
                                    {
                                        int contf = dsPermit.Tables[0].Columns.IndexOf("ord_qc");
                                        e.Row.Cells[contf].Text = failaux.Tables[1].Rows[i]["agent"].ToString();
                                        if (failaux.Tables[1].Rows[i]["completed"].ToString().Trim() == "NO")
                                        {
                                            e.Row.Cells[contf].Attributes.Add("Style", "background: none;");
                                        }
                                    }
                                }
                                else if (failaux.Tables[1].Rows[i]["fail_id"].ToString() == "5")
                                {
                                    if (dsPermit.Tables[0].Columns.Contains("Proc"))
                                    {
                                        if (failaux.Tables[1].Rows[i]["completed"].ToString().Trim() == "NO")
                                        {
                                            e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Proc")].Attributes.Add("Style", "background: none;");
                                        }
                                    }
                                }
                                else if (failaux.Tables[1].Rows[i]["fail_id"].ToString() == "0")
                                {
                                    if(dsPermit.Tables[0].Columns.Contains("Status"))
                                        e.Row.Cells[dsPermit.Tables[0].Columns.IndexOf("Status")].Text = "<img src=\"..\\img\\FailedYellow.png \" align=\"center\" >";
                                }

                                break;
                            }
                        }
                    }
                }
                catch (Exception ee)
                {
                    Utility.writeError(ee.Message);
                }
            }

        }
    }


    protected void gv_status_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
   
    protected void gv_status_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        /*if (e.Row.RowType == DataControlRowType.Header ) {
            e.Row.Cells[1].Width = 50;
            e.Row.Cells[1].Text = "";
            e.Row.Cells[2].Style.Add("display","none");
            e.Row.Cells[4].Style.Add("display", "none");//hide color
            e.Row.Cells[3].Width = 100;
            e.Row.Cells[3].Text = "Summary";
            e.Row.Cells[5].Text = "";
            e.Row.Cells[5].Width = 50;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[2].Style.Add("display", "none");
            e.Row.Cells[4].Style.Add("display", "none");
         
             e.Row.Cells[1].Width = 50;//color
             e.Row.Cells[3].Width = 100;//status
             e.Row.Cells[5].Width = 50;//count
             e.Row.Cells[1].Text = "<img src=\""+e.Row.Cells[1].Text+" \" align=\"middle\" >";
             e.Row.Cells[3].Text = "<A href=\"WorkSpace.aspx?get=" + e.Row.Cells[3].Text.ToLower() + "\">" + e.Row.Cells[3].Text + " </A>";
             if (((ruser.getPermiso() & 8) == 8) || ((long.Parse(ruser.getPermisoUser().ToString()) & 8) == 8))
             {
                 
                 if (e.Row.Cells[3].Text.ToUpper().Equals("PAID")){
                 e.Row.Cells[3].Text = "<A href=\"WorkSpace.aspx?get=paid\">"  + e.Row.Cells[3].Text + " </A>";
                 }
             }
            
        }*/
    }


   

    int GetSortColumnIndex()
    {

        foreach (DataControlField field in gv_detail.Columns)
        { 
            if (field.SortExpression == gv_detail.SortExpression)
            {
                return gv_detail.Columns.IndexOf(field);
            }
        }

        return -1;
    }
    protected void sorting(object sender, GridViewSortEventArgs e)
    {
        if (Session["dsPermit"] != null)
            dsPermit = (DataSet)Session["dsPermit"];

        if (dsPermit != null)
        {
            if (!e.SortExpression.Equals("County") || (!e.SortExpression.Equals("Order #")) || (!e.SortExpression.Equals("Due Date")) || (!e.SortExpression.Equals("Due Time")) || (!e.SortExpression.Equals("Type")) || (!e.SortExpression.Equals("Street No.")) || (!e.SortExpression.Equals("Street Address")) || (!e.SortExpression.Equals("City")) || (!e.SortExpression.Equals("State")) || (!e.SortExpression.Equals("Zip")))
          {
            DataView dv = (DataView)(dsPermit.Tables["Tabla"].DefaultView);
            if ((sort % 2) == 0)
            {
                dv.Sort = e.SortExpression + " " + "ASC";
                PTitle.BackColor = System.Drawing.Color.LightGray;
            }
            else
            {
                dv.Sort = e.SortExpression + " " + "DESC";
                PTitle.BackColor = System.Drawing.Color.LightGray;
            }
           // dv.Sort = e.SortExpression+" "+ ConvertSortDirection(e.SortDirection) ;
            sort++;
            gv_detail.DataSource = dv;
            
            gv_detail.DataBind();
          }
        }
    }

    private string ConvertSortDirection(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;
        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }
    protected void sorted(object sender, EventArgs e)
    {
       
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        
    }

    private int restricted()
    {
        int result = 0;
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
            RE_GenericBean permi = Utility.getPermisobyRuser(ruser, 1);
            permiso = permi.int64C2;
        }

        if ((permiso & 1) != 1) { result = 1; }
        if ((permiso & 16) == 16) { linkcamara.Visible = false; }
        if ((permiso & 8) == 8) { lnk_duedate.Visible = false; }
        if ((permiso & 32) == 32) { imalert.Visible = false; lalert.Visible = false; }
        if ((permiso & 128) == 128) { lnk_bposticket.Visible = false; }
        if ((permiso & 256) == 256) { punassigned.Visible = false; }
        if ((permiso & 512) == 512) { passigned.Visible=false; }
        if ((permiso & 1024) == 1024) { pprocessed.Visible=false; }
        if ((permiso & 16384) == 16384) { pready.Visible = false; }
        if ((permiso & 2048) == 2048) { pphotosunassigned.Visible = false; }
        if ((permiso & 4096) == 4096) { pphotosassigned.Visible = false; }
        if ((permiso & 8192) == 8192) { pphotosin.Visible = false; }
        if ((permiso & 65536) == 65536) { Photos.Visible = false; }
        if ((permiso & 131072) == 131072) { pfailed.Visible = false; }
        if ((permiso & 32768) == 32768) { Pprocess.Visible = false; }
        if ((permiso & 524288) == 524288) { PanelContador.Visible = false; }
        if ((permiso & 1048576) == 1048576) { pcounterclient.Visible = false; }
        if ((permiso & 8796093022208) == 8796093022208) { P_viewcart.Visible = false; }
        /*if ((permiso & 65536) == 65536) { Response.Redirect("OrderPlacement.aspx"); }
        if ((permiso & 131072) == 131072) { Response.Redirect("Pro.aspx"); }
        if ((permiso & 262144) == 262144) { Response.Redirect("sparkweb/SparkWeb.html"); }//Chat
        if ((permiso & 524288) == 524288) { Response.Redirect("#"); }//Invoices
        if ((permiso & 1048576) == 1048576) { Response.Redirect("#"); }//Admin
        if ((permiso & 2097152) == 2097152) { Response.Redirect("#"); }//Search*/

        pintarContador();
        return result;
    }

    private void permisos()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
            permiso = 0;
            foreach (RE_GenericBean per in user.permisos) { 
                if (per.intC1==1){ //Porque WK es Pagina 1
                    permiso = per.int64C1;
                }
            }
        }
      
        if ((permiso & 1) != 1) { gv_detail.Enabled = false; }
        if ((permiso & 16) != 16) { linkcamara.Enabled = false; }
        if ((permiso & 8) != 8) { lnk_duedate.Enabled = false; }
        if ((permiso & 32) != 32) { lalert.Enabled = false;}
        if ((permiso & 128) != 128) { lnk_bposticket.Enabled = false; }
        if ((permiso & 256) != 256) { punassigned.Enabled = false; }
        if ((permiso & 512) != 512) { passigned.Enabled = false; }
        if ((permiso & 1024) != 1024) { pprocessed.Enabled = false; }
        if ((permiso & 16384) != 16384) { pready.Enabled = false; }
        if ((permiso & 2048) != 2048) { pphotosunassigned.Enabled = false; }
        if ((permiso & 4096) != 4096) { pphotosassigned.Enabled = false; }
        if ((permiso & 8192) != 8192) { pphotosin.Enabled = false; }
        if ((permiso & 65536) != 65536) { Photos.Enabled = false; }
        if ((permiso & 131072) != 131072) { pfailed.Enabled = false; }
        if ((permiso & 32768) != 32768) { Pprocess.Enabled = false; }
        if ((permiso & 524288) != 524288) { PanelContador.Enabled = false; }
        if ((permiso & 1048576) != 1048576) { pcounterclient.Enabled = false; }
        if ((permiso & 8796093022208) != 8796093022208) { P_viewcart.Enabled = false; }
        if ((PWK & 70368744177664) != 70368744177664) { lnk_pending.Enabled = false; }
        if ((PWK & 140737488355328) != 140737488355328) { lnk_completed.Enabled = false; }
        if ((PWK & 281474976710656) != 281474976710656) { lnk_cancelled.Enabled = false; }
        if ((PWK & 1048576) != 1048576) { lnk_pending.Enabled = false; lnk_completed.Enabled = false; lnk_cancelled.Enabled = false; }
        if ((PWK & 32768) != 32768) { HyperLink1.Enabled = false; }
        if ((PWK & 65536) != 65536) { HyperLink7.Enabled = false; HyperLink2.Enabled = false; }
        if ((PWK & 131072) != 131072) { HyperLink6.Enabled = false; HyperLink4.Enabled = false; }
        if ((PWK & 35184372088832) != 35184372088832) { HyperLink1.Enabled = false; HyperLink6.Enabled = false; HyperLink7.Enabled = false; }
    }

    private void pintarContador()
    {
        string[] pcont = new string[7] { "punassigned", "pphotosunassigned", "passigned", "pphotosassigned", "pprocessed", "pphotosin", "pready" };
        int cont = 0;
        for (int a = 0; a < pcont.Length; a++)
        {
            Panel paux = (Panel)PanelContador.FindControl(pcont[a]);
            if (paux != null)
            {
                if (paux.Visible.ToString().ToUpper() == "TRUE")
                {
                    cont++;
                    if (cont % 2 == 0)
                    {
                        paux.BackColor = System.Drawing.Color.FromName("#CBE2FE");
                    }
                }
            }
        }
    }

   
    
    protected void btn_search_Click(object sender, ImageClickEventArgs e)

    {
        
    }
   
    protected void lnk_duedate_Click(object sender, EventArgs e)
    {
        string nomdia, dia, dia2, fecha, fecha2;
        nomdia = DateTime.Now.AddDays(1).DayOfWeek.ToString();
        //dia = DateTime.Now.AddDays(1).Day.ToString();
        //fecha = DateTime.Now.AddDays(1).Month.ToString() + "/" + dia + "/" + DateTime.Now.AddDays(1).Year;

        if (nomdia == "Saturday")
        {
            dia2 = DateTime.Now.AddDays(4).Day.ToString();
            fecha2 = DateTime.Now.AddDays(4).Month.ToString() + "/" + dia2 + "/" + DateTime.Now.AddDays(4).Year;
        }
        else
        {
            dia2 = DateTime.Now.AddDays(2).Day.ToString();
            fecha2 = DateTime.Now.AddDays(2).Month.ToString() + "/" + dia2 + "/" + DateTime.Now.AddDays(2).Year;
        }

        //int res = DB.changeDuedate(fecha2, fecha);
        int res = DB.changeDuedate(fecha2);
        Response.Redirect("WorkSpace.aspx");
    }

    protected void gv_detail_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        if (Session["dsPermit"] != null)
            dsPermit = (DataSet)Session["dsPermit"];

        if (dsPermit != null)
        {
            gv_detail.PageIndex = e.NewPageIndex;
            gv_detail.DataSource = dsPermit.Tables["Tabla"];
            gv_detail.DataBind();
        }
    }
    protected void lnk_wksearch_Click(object sender, EventArgs e)
    {
        buscar();
        
        }

    private void buscar()
    {
        try
        {
            string where = "";
            if (tb_search.Text != "")
            {
                tb_search.Text = tb_search.Text.Trim();
            }
            string message = "";
            if (!tb_search.Text.Equals(""))
            {


                where = " and (orden.stnum like '" + tb_search.Text + "' or orden.ordernum= '" + tb_search.Text + "' )";
                message = " **** Order# or  Street# " + tb_search.Text;
            }



            Session["wheress"] = where;
            Session["SingleSearch"] = 1;
            Session["message"] = message + " not Found **** ";
            Response.Redirect("WorkSpace.aspx");

        }
        catch (Exception ex)
        {
            Response.Write("<script> alert('Invalid criteria for Search');</script>");
        }
    }


    protected void lnk_wkok_Click(object sender, EventArgs e)
    {
        alert_ModalPopupExtender.Hide();
        Response.Redirect("WorkSpace.aspx");
    }

}
